package models.utils
{
	import common.cacher.PlayerCacher;
	
	import kernel.core.Core;
	
	import proto.client.Player;

	/**
	 * 多个队列请求，并在全部数据到达后回调函数，函数接收 Player 组成的数组 
	 * @author flashk
	 * 
	 */
	public class PlayerInfoGet
	{

		private var _callBack:Function;
		private var _count:uint;
		private var _ids:Array;
		private var _players:Array;
		private var _states:Array;
		
		public function PlayerInfoGet()
		{
			//
		}
		
		public function get players():Array
		{
			return _players;
		}
		public function getPlayerAt(index:uint):Player
		{
			return _players[index] as Player;
		}
		
		public function getByIDs(ids:Array,callBack:Function):void
		{
			_count = 0;
			_callBack = callBack;
			_ids = ids;
			_players = [];
			_states = [];
			PlayerCacher.instance.getPlayerList(ids,checkGetAll);
		}
		
		private function checkGetAll(player:Player):void
		{
			var index:uint;
			for(var i:int=0;i<_ids.length;i++)
			{
				if( String(_ids[i]) == player.id.toString()  && _states[i] != true )
				{
					index = i;
					break;
				}
			}
			_states[index] = true;
			_players[index] = player;
			_count++;
			if(_count >= _ids.length)
			{
				_callBack(_players);
			}
		}
		
	}
}